package com.woniu.rabbit.dlx_model.exchange;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class DlxDirectConfig {
    public static final String DLX_DIRECT_EXCHANGE = "dlx_direct_exchange";
    public static final String DLX_DIRECT_NORMAL_EXCHANGE = "normal_direct_exchange";
    public static final String DLX_DIRECT_QUEUE = "dlx_direct_queue";
    public static final String DLX_DIRECT_NORMAL_QUEUE = "dlx_direct_normal_queue";
    public static final String DLX_DIRECT_ROUTING_KEY = "dlx_direct_routing_key";

    /**
     * 创建普通交换机 normal.direct
     */
    @Bean
    public DirectExchange normalDirectExchange() {
        return new DirectExchange(DLX_DIRECT_NORMAL_EXCHANGE);
    }

    /**
     * 创建死信交换机 dlx.direct
     */
    @Bean
    public DirectExchange dlxDirectExchange() {
        log.info("创建死信交换机：{}", DLX_DIRECT_EXCHANGE);
        return new DirectExchange(DLX_DIRECT_EXCHANGE);
    }

    /**
     * 创建普通队列 dlx.direct.queue
     */
    @Bean
    public Queue normalDirectQueue() {
        log.info("创建普通队列：{}", DLX_DIRECT_NORMAL_QUEUE);
        // 指定 队列名称、死信交换机、死信路由键
        return QueueBuilder
                .durable(DLX_DIRECT_NORMAL_QUEUE) // 指定 队列名称
                .deadLetterExchange(DLX_DIRECT_EXCHANGE)    // 指定 死信交换机
                .deadLetterRoutingKey(DLX_DIRECT_ROUTING_KEY)   // 指定 死信路由键
                .build();
    }
    /**
     * 创建死信队列
     * */
    @Bean
    public Queue dlxDirectQueue() {
        log.info("创建死信队列：{}", DLX_DIRECT_QUEUE);
        return new Queue(DLX_DIRECT_QUEUE);
    }


    /**
     * 绑定normal.direct
     */
    @Bean
    public Binding normalDirectBinding(DirectExchange normalDirectExchange, Queue normalDirectQueue) {
        return BindingBuilder.bind(normalDirectQueue)
                .to(normalDirectExchange)
                .with("normal.key");
    }

    /**
     * 绑定死信队列
     * */
    @Bean
    public Binding dlxDirectBinding(DirectExchange dlxDirectExchange, Queue dlxDirectQueue) {
        return BindingBuilder.bind(dlxDirectQueue)
                .to(dlxDirectExchange)
                .with(DLX_DIRECT_ROUTING_KEY);
    }

}
